//
//  ProductNew.m
//  Trading365
//
//  Created by Jason on 12-3-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "InfoAdd.h"

@implementation InfoAdd
@synthesize table;
@synthesize InfoListTitle,InfoListName;
@synthesize InfoNewDict;
@synthesize parentID,guid;

static CGFloat theKeyboardHeight = 216.0f;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title=@"";
    [self.view setBackgroundColor:BACKGROUND_COLOR];

    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]
                                initWithTitle:@"保存"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(save)];
    
    self.navigationItem.rightBarButtonItem = saveBtn;
    [saveBtn release];
    
    NSArray *title_array = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListTitle = title_array; 
    [title_array release];

    NSArray *name_title = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListName = name_title; 
    [name_title release];
    
    InfoNewDict = nil;
    if ([[InfoType share] GetTranValueDict] != nil)
        InfoNewDict = [[InfoType share]GetTranValueDict];
    
    //Jason需要重写
//    [[InfoType share] SetFlagInfoType:INFO_TYPE_PRODUCT_ADD];
    
    [self refreshData];
    [self.table reloadData];

//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    tableFrame=self.table.frame;
    
    keyboardHeight = theKeyboardHeight - 49;
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [InfoNewDict release];
    [parentID release];
    [guid release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ///////////////////////////
    [self.table reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    self.InfoListTitle=nil;
    self.InfoListName = nil;
    InfoNewDict = nil;
    parentID = nil;
    
    [table release];
    [super dealloc];
}

#pragma mark - Table view data source
//Jason需要重写
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    
/*    if (row==0) {
        static NSString *CellIdentifier = @"ImageListCell";
        
        ImageListCell* cell = (ImageListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ImageListCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            
        }
        
        cell.picture.image = [UIImage imageNamed:@"product.jpg"];
        cell.picture.userInteractionEnabled=YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picture:)];
        [cell.picture addGestureRecognizer:singleTap];
        [singleTap release];
              
        cell.name.textAlignment =  UITextAlignmentRight;
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView.backgroundColor = [UIColor clearColor];
        
        return cell;
        
    }
*/    
    static NSString *CellIdentifier = @"InfoEditCell";
    
    InfoEditCell* cell = (InfoEditCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"InfoEditCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];

    //allow edit
    cell.textValue.userInteractionEnabled=YES;
    cell.cellButton.hidden = YES;
    if ([self.InfoListTitle count] > row){
        cell.labName.text = [self.InfoListTitle objectAtIndex:row];
    }
    cell.textValue.delegate = (id)self;
/*    
    if (InfoNewDict != nil){
        if (row == 1){
            cell.labValue.text = [InfoNewDict objectForKey:INFO_NAME_PRODUCT_NAME];
        }
        if (row == 2){
            cell.labValue.text = [InfoNewDict objectForKey:INFO_NAME_PRODUCT_CATEGORY];
        }
        if (row == 3){
            cell.labValue.text = [InfoNewDict objectForKey:INFO_NAME_PRODUCT_UNIT];
        }
        if (row == 4){
            cell.labValue.text = [InfoNewDict objectForKey:INFO_NAME_PRODUCT_CUSTOM_CODE];
        }
    }
    
    if (row == 2 || row == 3 || row == 4)
    {
        cell.labValue.userInteractionEnabled = NO;
        cell.cellButton.hidden = NO;
        [cell.cellButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
*/        
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

//Jason需要重写
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 60;
    }
    else
        return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.InfoListTitle count];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    
}

//#pragma mark - keyboardWillShow
//- (void)keyboardWillShow:(NSNotification *)notification
//{
////    NSDictionary *info = [notification userInfo];
////    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
////    NSLog(@"kbSize.height=%f;kbSize.width=%f",kbSize.height,kbSize.width);
////    CGFloat distanceToMove = kbSize.height - normalKeyboardHeight;
////    keyboardHeight = kbSize.height-49;
//    
//    //自适应代码
//}

//#pragma mark - text delegate
////该方法为点击输入文本框要开始输入是调用的代理方法：就是把view上移到能看见文本框的地方
//- (void)textFieldDidBeginEditing:(UITextField *)textField{
//    [UIView beginAnimations:@"srcollView" context:nil];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    [UIView setAnimationDuration:0.275f];
//    self.table.frame = CGRectMake(self.table.frame.origin.x, 0, self.table.frame.size.width, self.table.frame.size.height-keyboardHeight);
//    [UIView commitAnimations];
//}
//
////该方法为点击虚拟键盘Return，要调用的代理方法：隐藏虚拟键盘
//- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//    [textField resignFirstResponder];
//    return YES; 
//}
//
////该方法为完成输入后要调用的代理方法：虚拟键盘隐藏后，要恢复到之前的文本框地方
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//    [UIView beginAnimations:@"srcollView" context:nil];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    [UIView setAnimationDuration:0.275f];
//    self.table.frame = CGRectMake(self.table.frame.origin.x, 0, self.table.frame.size.width, self.table.frame.size.height + keyboardHeight);
//    [UIView commitAnimations];
//    
//    [textField resignFirstResponder];
//}


#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    //    [super OnPreExcute];
}

//Jason需要重写
-(void) DoInBackground
{    
    [self.table reloadData];
}

-(void) OnPostExcute
{
    //    [super OnPostExcute];
}

//Jason需要重写
-(void)refreshData
{    
//    [[[TaskManager Share] GetTaskByType:NewProduct Delegate:self] Excute];
}


//Jason需要重写
- (void)buttonTapped:(id)sender
{    
//    InfoEditCell * cell = (InfoEditCell *)[[sender superview] superview];
//    NSIndexPath * path = [self.table indexPathForCell:cell];
//    NSUInteger buttonRow=path.row;
    
/*    InfoSelectList *selectList = [[InfoSelectList alloc] init];
    
    //@"产品类别",
    if (buttonRow == 2)
    {
        [selectList SetInfoType:INFO_TYPE_PRODUCT_CATEGORY];   
    }
    //@"单位",
    if (buttonRow == 3)
    {
        [selectList SetInfoType:INFO_TYPE_PRODUCT_UNIT];
    }
    //@"海关编码",
    if (buttonRow == 4)
    { 
        [selectList SetInfoType:INFO_TYPE_PRODUCT_CUSTOM_CODE];
    }
    
    [self.navigationController pushViewController:selectList animated:YES];
    [selectList release];
*/
}

-(void)picture:(id)sender{
    
    NSLog(@"take picture....");

    if ([[InfoType share] IsSimulator])
        return;
    
    //有问题，图像没有即时回传给imageview
    
    ProductTakePict *picture = [[ProductTakePict alloc] init];
    //    [detail setKey_id:p_id];
    [self.navigationController pushViewController:picture animated:YES];
    
    [picture release];
    
}

//Jason需要重写
-(void)save{
    //
//    NSLog(@"InfoNewDict=%@",InfoNewDict);
//    NSString *alert=@"";
/*    
    if ([InfoNewDict objectForKey:INFO_NAME_PRODUCT_UNIT_ID] == nil)
        alert=@"请选择产品单位.";
    
    if ([InfoNewDict objectForKey:INFO_NAME_PRODUCT_CATEGORY_ID] == nil)
        alert=@"请选择产品类别.";
    
    if ([InfoNewDict objectForKey:INFO_NAME_PRODUCT_NAME] == nil)
        alert=@"请输入产品名称.";

    if ([alert length]>0){
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:alert delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        [alertview release];
    }
 */
}

@end
